package service

import (
	"context"
    "testing"
	"github.com/liuxd6825/dapr-go-ddd-sdk/utils/randomutils"
	"{{.Namespace}}/pkg/cmd-service/application/internals/{{.aggregate_name}}/appcmd"
	"{{.Namespace}}/pkg/cmd-service/domain/{{.aggregate_name}}/field"
	"{{.Namespace}}/pkg/cmd-service/infrastructure/test"
)

func Test{{.Name}}AppCmdService(t *testing.T) {
	ctx := context.Background()
	service := New{{.Name}}AppCmdService()

	id := randomutils.UUID()
    tenantId:= test.TenantId
	{{- range $i, $cmd := .Aggregate.Commands }}

	t.Run("{{$cmd.Name}}", func(t *testing.T) {
		cmd := &appcmd.{{$cmd.AppName}}{}
        cmd.CommandId = randomutils.UUID()
        cmd.IsValidOnly = false
		cmd.Data = field.{{$cmd.FieldsName}}{
        {{- range $name, $property := $cmd.Fields.Properties}}
            {{- if $property.NameIsId }}
            Id: id,
            {{- else if $property.IsItems }}
                {{- range $v, $itemCode := $property.GoAppTestItemsValue }}
                {{ $itemCode }}
                {{- end }}
            {{- else }}
            {{$name}} : {{$property.GoAppTestValue}},
            {{- end }}
        {{- end }}
		}

        if err := test.PrintJson(t, "{{$cmd.AppName}}", cmd); err != nil {
            t.Error(err)
        }

		if err := service.{{$cmd.ServiceFuncName}}(ctx, cmd); err != nil {
			t.Error(err)
		}
	})

	{{- end }}

	t.Run("FindAggregateById", func(t *testing.T) {
		agg, ok, err := service.FindAggregateById(ctx, tenantId, id)
		if err != nil {
			t.Error(err)
			return
		} else if ok {
            if err := test.PrintJson(t, "FindAggregateById", agg); err != nil {
                t.Error(err)
            }
		} else {
			t.Log("not found")
		}
	})
}
